#include "leiliconfig.h"

LeiliConfig::LeiliConfig(QObject *parent) : QObject(parent)
{

}

void LeiliConfig::load(QString path)
{

    m_settings = new QSettings(path, QSettings::IniFormat, this);
    m_settings->setIniCodec("UTF-8");


    m_settings->beginGroup("scale");
    QVariant defaultValue = 1.5;
    QVariant value = m_settings->value("grandpan_dia", defaultValue);
    m_grindingPanDiameter = value.toDouble();

    defaultValue = 0.1;
    value = m_settings->value("overlap_width", defaultValue);
    m_overlapWidth = value.toDouble();


    defaultValue = 3;
    value = m_settings->value("robot_num", defaultValue);
    m_robotNum = value.toInt();

    defaultValue = 0.3;
    value = m_settings->value("grid_limitmin", defaultValue);
    m_gridLimitMin = value.toDouble();

    defaultValue = 1;
    value = m_settings->value("grid_limitmax", defaultValue);
    m_gridLimitMax = value.toDouble();

    m_settings->endGroup();

    m_settings->beginGroup("yaml");
    defaultValue = "./";
    value = m_settings->value("save_path", defaultValue);
    m_savePath = value.toString();
    m_settings->endGroup();
}

void LeiliConfig::save()
{
    writeValue("scale","grandpan_dia",m_grindingPanDiameter);
    writeValue("scale","overlap_width",m_overlapWidth);
    writeValue("scale","robot_num",m_robotNum);
    writeValue("scale","grid_limitmin",m_gridLimitMin);
    writeValue("scale","grid_limitmax",m_gridLimitMax);

    writeValue("yaml","save_path",m_savePath);

    writeValue("yaml","save_path",m_savePath);
    sync();
}

void LeiliConfig::writeValue(const QString &section, const QString &key, const QVariant &value)
{
    m_settings->beginGroup(section);
    m_settings->setValue(key, value);
    m_settings->endGroup();
}

void LeiliConfig::setGrindingPanDiameter(double dia)
{
    if(dia <=0)
    {
        return;
    }

    m_grindingPanDiameter = dia;
}

double LeiliConfig::getGrindingPanDiameter()
{
    return m_grindingPanDiameter;
}

void LeiliConfig::setOverlapWidth(double width)
{
    if(width <=0)
    {
        return;
    }

    m_overlapWidth  = width;
}

double LeiliConfig::getOverlapWidth()
{
    return m_overlapWidth;
}

void LeiliConfig::setRobotNum(int robotNum)
{
    if(robotNum <= 0)
    {
        return;
    }
    m_robotNum = robotNum;
}

int LeiliConfig::getRobotNum()
{
    return m_robotNum;
}

void LeiliConfig::setSavePath(QString path)
{
    m_savePath = path;
}

QString LeiliConfig::getSavePath()
{
    return m_savePath;
}

void LeiliConfig::setGridLimitMin(double val)
{
    if(val < 0)
    {
        return;
    }

    m_gridLimitMin = val;
    if(m_gridLimitMin >= m_gridLimitMax)
    {
        m_gridLimitMax = m_gridLimitMin + 0.1;
    }
}

double LeiliConfig::getGridLimitMin()
{
    return m_gridLimitMin;
}

void LeiliConfig::setGridLimitMax(double val)
{
    if(val <=0 || val < m_gridLimitMin)
    {
        return;
    }

    m_gridLimitMax = val;
}

double LeiliConfig::getGridLimitMax()
{
    return m_gridLimitMax;
}

void LeiliConfig::sync()
{
    return m_settings->sync();
}
